-------------------------------------------------------
--Baldy Bignose                                       -
--player.lua                                          -
--Provides various functions for managing the player  -
--Thomas Young  7/10/2012                             -
-------------------------------------------------------
require 'gamebase.basegameobject'
require 'gamebase.sprite'

player = {}
	
	player.objdata = basegameobject
	player.spr = sprite
	
	player.curfallspeed = 0
	player.maxfallspeed = 2
	player.curjumptime = 0
	player.maxjumptime = 3
	player.jumpincrement = 0.09
	
	player.curspeed = 0.0
	player.maxrunspeed = 2.0
	player.acceleration = 0.2
	
	player.direction = -1
	
	player.isJumping = false
	player.isFalling = true

function player:Init()
	self.objdata:Init(0,0,16,26,"Player", "player")
	self.spr:Init(16,26,3, "content/sprites/baldy.png")
end

function player:Update(dt)
	--update stuff goes here
	self.spr:Update(dt)
	
	self:vertMovement()
	
	self:horzMove()
end

function player:vertMovement()
	if (self.isJumping == false) then
		if (self.objdata.y < love.graphics.getHeight() - self.objdata.h) then
			self.isFalling = true
			self.curfallspeed = self.curfallspeed + self.acceleration
			self.spr.frame = 3
		else
			self.isFalling = false
			self.curfallspeed = 0
			self.objdata.y = love.graphics.getHeight() - self.objdata.h
		end
	end
	
	if (self.isJumping == false) and love.keyboard.isDown("up")	and (self.isFalling == false) then
		self.isJumping = true -- start jumping
		self.curjumptime = 0
		self.curfallspeed = -1
	end
	
	if (self.isJumping == true) then
			if love.keyboard.isDown("up") then
				self.curfallspeed = self.curfallspeed + self.acceleration * (-1)
				self.spr.frame = 3
				--do the jump timer
				if (self.curjumptime < self.maxjumptime) then
					self.curjumptime = self.curjumptime + self.jumpincrement
				else
				self.isJumping = false
				end
				
			else
				self.isJumping = false
				
			end
	end
	--prevents from jumping/falling too fast
	if (self.isFalling) and (self.curfallspeed > self.maxfallspeed) then
	
		self.curfallspeed = self.maxfallspeed
	end
	if (self.isJumping) and (self.curfallspeed < self.maxfallspeed*-1) then
		self.curfallspeed = self.maxfallspeed*-1
	end
	self.objdata.y = self.objdata.y + self.curfallspeed
end

-- Controls movement and acceleration on the x axis
function player:horzMove()
	if love.keyboard.isDown("left") then
		if (self.curspeed > self.maxrunspeed*(-1)) then
			self.curspeed = self.curspeed - self.acceleration
		end
		self.direction = 1
		self.spr.row = 1
	elseif love.keyboard.isDown("right") then
		if (self.curspeed < self.maxrunspeed) then
			self.curspeed = self.curspeed + self.acceleration
		end
		self.direction = 0
		self.spr.row = 0
	else
		if (self.curspeed < 0) then
			self.curspeed = 0
		end
		if (self.curspeed > 0) then
			self.curspeed = 0
		end
		self.direction = (-1)
		if (self.isFalling == false or self.isJumping == false) then
			self.spr.frame = 0
		end
	end
	
	self.objdata.x = self.objdata.x + self.curspeed
end

function player:Draw()
	--draw the players sprite and any other graphics related to player
	self.spr:Draw(self.objdata.x, self.objdata.y)
end

function player:Collision()
--do collision stuff here
--probably will use a switch for each collision case
end